"""
@name : 文件操作作业
@author : FanMY_71
@projectname: Pycharm练习
"""
# 第二题

# import random
# from collections import Counter
# with open("test.txt","a+") as fp:
#     fp.truncate(0)  # 清空文本的内容
#     i = 0
#     while i < 100:
#         i += 1
#         lucky = random.randint(1,255)
#         fp.write(f"172.25.254.{lucky}\n")
#     fp.seek(0)
#     biglst = fp.readlines()
#     counter = Counter(biglst)
#     most_arr = sorted(counter.items(), key=lambda pair: pair[1], reverse=True)
#     print(most_arr[0:10])


##################################################
# 第二题
# import string
#
# with open('Walden.txt', 'r',encoding="utf-8-sig") as file:  # 换成"utf-8-sig"避免了"\ufeff"（字节序）的出现
#     article = file.read().lower()
#     # 找出这些特殊字符,!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
#     _punctuation = string.punctuation
#     # print(_punctuation)
#     no_pun_text = article
#     for pun in _punctuation:
#         no_pun_text = no_pun_text.replace(pun,'')
#     print(no_pun_text)
#     records = dict()
#     # 方法通过指定分隔符对字符串进行分割并返回一个列表，默认分隔符为空字符，包括空格，换行(\n), 制表符(\t)等
#     for word in no_pun_text.split():
#         records[word] = records.get(word, 0) + 1  # 若是找到了一样的word就让这个键值+1
#     # 把字典的键值转化为一个列表,然后根据列表的每一个元素下标为"1"的值的大小进行排序，这个函数出来就是列表，reverse=True，是降序
#     items = sorted(records.items(), key=lambda x: x[1], reverse=True)
#


